<?php
require_once 'common/config.php';
require_once 'common/functions.php';

// 检查登录状态
check_login();

$db = db_connect();

// 获取教练列表
$coaches_sql = "SELECT * FROM coaches WHERE status = 1";
$coaches_stmt = $db->query($coaches_sql);
$coaches = $coaches_stmt->fetchAll(PDO::FETCH_ASSOC);

// 预定义时间段
$time_slots = [
    '09:00-10:00',
    '10:00-11:00',
    '11:00-12:00',
    '14:00-15:00',
    '15:00-16:00',
    '16:00-17:00',
    '17:00-18:00',
    '18:00-19:00',
    '19:00-20:00'
];

// 处理表单提交
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $coach_id = intval($_POST['coach_id']);
    $date = $_POST['date'];
    $time_slots = isset($_POST['time_slots']) ? $_POST['time_slots'] : [];
    $is_available = isset($_POST['is_available']) ? 1 : 0;

    // 批量插入排班记录
    foreach ($time_slots as $time_slot) {
        // 检查是否已存在排班
        $check_sql = "SELECT * FROM coach_schedules WHERE coach_id = :coach_id AND date = :date AND time_slot = :time_slot";
        $check_stmt = $db->prepare($check_sql);
        $check_stmt->bindValue(':coach_id', $coach_id);
        $check_stmt->bindValue(':date', $date);
        $check_stmt->bindValue(':time_slot', $time_slot);
        $check_stmt->execute();
        $existing_schedule = $check_stmt->fetch(PDO::FETCH_ASSOC);

        if (!$existing_schedule) {
            // 插入新排班
            $insert_sql = "INSERT INTO coach_schedules (coach_id, date, time_slot, is_available) 
                          VALUES (:coach_id, :date, :time_slot, :is_available)";
            $insert_stmt = $db->prepare($insert_sql);
            $insert_stmt->bindValue(':coach_id', $coach_id);
            $insert_stmt->bindValue(':date', $date);
            $insert_stmt->bindValue(':time_slot', $time_slot);
            $insert_stmt->bindValue(':is_available', $is_available);
            $insert_stmt->execute();
        }
    }

    // 重定向回列表页面
    header('Location: private_courses.php');
    exit;
}
?>

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>添加排班 - <?php echo SITE_NAME; ?></title>
    <link rel="stylesheet" href="assets/css/style.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">
    <style>
        .form-container {
            background: #fff;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.05);
        }

        .form-group {
            margin-bottom: 20px;
        }

        .form-group label {
            display: block;
            margin-bottom: 8px;
            font-weight: 500;
            color: #333;
        }

        .form-group select,
        .form-group input[type="date"] {
            width: 100%;
            padding: 8px 12px;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 14px;
            transition: all 0.3s;
        }

        .form-group select:focus,
        .form-group input[type="date"]:focus {
            border-color: #3498db;
            box-shadow: 0 0 0 2px rgba(52,152,219,0.2);
        }

        .time-slots {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
            gap: 10px;
            margin-top: 10px;
        }

        .time-slot-item {
            display: flex;
            align-items: center;
            gap: 8px;
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
            cursor: pointer;
            transition: all 0.3s;
        }

        .time-slot-item:hover {
            background: #f8f9fa;
        }

        .time-slot-item input[type="checkbox"] {
            margin: 0;
        }

        .btn-container {
            margin-top: 20px;
            display: flex;
            gap: 10px;
        }

        .btn {
            padding: 10px 20px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 14px;
            transition: all 0.3s;
        }

        .btn-primary {
            background: #3498db;
            color: #fff;
        }

        .btn-primary:hover {
            background: #2980b9;
        }

        .btn-secondary {
            background: #6c757d;
            color: #fff;
        }

        .btn-secondary:hover {
            background: #5a6268;
        }
    </style>
</head>
<body>
    <div class="wrapper">
        <?php include 'includes/sidebar.php'; ?>
        
        <div class="main-content">
            <header class="top-header">
                <div class="header-left">
                    <h2>添加排班</h2>
                </div>
            </header>

            <div class="content">
                <div class="form-container">
                    <form action="" method="POST">
                        <div class="form-group">
                            <label for="coach_id">选择教练</label>
                            <select name="coach_id" id="coach_id" required>
                                <option value="">请选择教练</option>
                                <?php foreach ($coaches as $coach): ?>
                                    <option value="<?php echo $coach['id']; ?>">
                                        <?php echo htmlspecialchars($coach['name']); ?>
                                    </option>
                                <?php endforeach; ?>
                            </select>
                        </div>

                        <div class="form-group">
                            <label for="date">选择日期</label>
                            <input type="date" name="date" id="date" required 
                                   min="<?php echo date('Y-m-d'); ?>">
                        </div>

                        <div class="form-group">
                            <label>选择时间段</label>
                            <div class="time-slots">
                                <?php foreach ($time_slots as $slot): ?>
                                    <label class="time-slot-item">
                                        <input type="checkbox" name="time_slots[]" value="<?php echo $slot; ?>">
                                        <?php echo $slot; ?>
                                    </label>
                                <?php endforeach; ?>
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="time-slot-item">
                                <input type="checkbox" name="is_available" value="1" checked>
                                设置为可用
                            </label>
                        </div>

                        <div class="btn-container">
                            <button type="submit" class="btn btn-primary">
                                <i class="fas fa-save"></i> 保存
                            </button>
                            <a href="private_courses.php" class="btn btn-secondary">
                                <i class="fas fa-arrow-left"></i> 返回
                            </a>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</body>
</html> 